package com.haierp.service.impl;

import com.baomidou.framework.service.impl.SuperServiceImpl;
import com.haierp.mapper.TaskDailyOrderMapper;
import com.haierp.model.ItemSku;
import com.haierp.model.TaskDailyOrder;
import com.haierp.model.TaskDailyOrderHistory;
import com.haierp.service.ITaskDailyOrderService;
import com.haierp.util.JsonPageResult;
import com.haierp.vo.ItemSkuQueryVO;
import com.wangzhixuan.commons.utils.BeanUtils;
import org.eclipse.jetty.util.StringUtil;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class TaskDailyOrderServiceImpl extends SuperServiceImpl<TaskDailyOrderMapper, TaskDailyOrder>  implements ITaskDailyOrderService {
	
	@Override
	public List<TaskDailyOrderHistory> createTaskDailyOrder() {
		List<TaskDailyOrder> taskDailyOrderList = this.baseMapper.queryTaskDailyDetailByOrder();
		List<TaskDailyOrder> taskDailyOrderListInsert = new ArrayList<TaskDailyOrder>();
		List<TaskDailyOrderHistory> autoTaskDailyOrderListInsert = new ArrayList<TaskDailyOrderHistory>();
		Date date = new Date();
		//剔除不需要采购的
		taskDailyOrderList.forEach(taskDailyOrder -> {
			if(taskDailyOrder.getInventory()==null || taskDailyOrder.getInventory()<0) taskDailyOrder.setInventory(0);
			if(taskDailyOrder.getTransInv()==null || taskDailyOrder.getTransInv()<0) taskDailyOrder.setTransInv(0);
			if(taskDailyOrder.getVirtualInv()==null || taskDailyOrder.getVirtualInv()<0) taskDailyOrder.setVirtualInv(0);
			if(taskDailyOrder.getLockedInv()==null || taskDailyOrder.getLockedInv()<0) taskDailyOrder.setLockedInv(0);
			if(taskDailyOrder.getLockedTransInv()==null || taskDailyOrder.getLockedTransInv()<0) taskDailyOrder.setLockedTransInv(0);
			
			int purchaseNeed = taskDailyOrder.getSaleNeed() - taskDailyOrder.getInventory() - taskDailyOrder.getTransInv();
			if(purchaseNeed < 0) {
				taskDailyOrder.setPurchaseNeed(0);
			} else {
				taskDailyOrder.setPurchaseNeed(purchaseNeed);
			}
			
			taskDailyOrder.setCurrentlyDate(date);
			taskDailyOrder.setGmtCreate(date);
			taskDailyOrder.setGmtModify(date);
			if(taskDailyOrder.getPurchaseNeed()>0 && StringUtil.isNotBlank(taskDailyOrder.getSkuCode())) {
				taskDailyOrderListInsert.add(taskDailyOrder);
			}
		});
		//删除之前的采购明细
		Map<String, Object> columnMap = new HashMap<String, Object>();
		this.baseMapper.deleteByMap(columnMap);
		//插入当天产生的采购明细
		if(taskDailyOrderListInsert.size()>0){
			this.baseMapper.insertBatch(taskDailyOrderListInsert);
			for (TaskDailyOrder taskDailyOrder:taskDailyOrderListInsert) {
				TaskDailyOrderHistory autoTaskDailyOrder = new TaskDailyOrderHistory();
				BeanUtils.copyProperties(taskDailyOrder,autoTaskDailyOrder);
				autoTaskDailyOrderListInsert.add(autoTaskDailyOrder);
			}
		}
		return autoTaskDailyOrderListInsert;
	}
	
	@Override
	public JsonPageResult<List<ItemSku>> queryItemSkus(ItemSkuQueryVO itemSkuQueryVO) {
		JsonPageResult<List<ItemSku>> itemResult = new JsonPageResult<>();
		//1、查询总的记录数量
		Integer totalCount =  this.baseMapper.queryItemSkusCount(itemSkuQueryVO);
		
		//2、查询分页记录
		if(totalCount!=null&&totalCount!=0L){
			itemResult.buildPage(totalCount, itemSkuQueryVO);
			List<ItemSku> itemSkus = this.baseMapper.queryItemSkus(itemSkuQueryVO);
			itemResult.setData(itemSkus);
		}
		return itemResult;
	}
	
	@Override
	public List<TaskDailyOrder> queryAll() {
		return this.baseMapper.queryAll();
	}
	
}
